package audio

import (
	"github.com/gin-gonic/gin"
	"moko/internal/services/audio"
	"net/http"
)

// HandleAppFileUpload 处理App端文件上传的处理器函数
func HandleAppFileUpload(c *gin.Context) {
	regRes := audio.HandleAppFileUpload(c)
	if regRes.Code != http.StatusOK {
		c.JSON(http.StatusInternalServerError, gin.H{"code": regRes.Code, "msg": regRes.Msg})
		return
	}

	c.JSON(http.StatusOK, gin.H{"code": http.StatusOK, "msg": "获取成功", "data": regRes.Data})

	return
}

func HandleAppFileUploadVideo(c *gin.Context) {
	regRes := audio.HandleAppFileUploadVideo(c)
	if regRes.Code != http.StatusOK {
		c.JSON(http.StatusInternalServerError, gin.H{"code": regRes.Code, "msg": regRes.Msg})
		return
	}

	c.JSON(http.StatusOK, gin.H{"code": http.StatusOK, "msg": "获取成功", "data": regRes.Data})

	return
}

// WebSocket 长链接处理函数
func WebSocket(c *gin.Context) {
	audio.HandleWebSocket(c)
}

// PushSingle 发送单条数据
func PushSingle(c *gin.Context) {
	clientID := c.PostForm("client_id")
	message := []byte(c.PostForm("message"))
	err := audio.SendMessageToClient(clientID, message)
	if err != nil {
		c.JSON(400, gin.H{"error": err.Error()})
		return
	}
	c.JSON(200, gin.H{"message": "消息已发送"})
}

// PushBroadcast 发送广播数据
func PushBroadcast(c *gin.Context) {
	message := []byte(c.PostForm("message"))
	audio.BroadcastMessage(message)
	c.JSON(200, gin.H{"message": "消息已广播"})
}
